/*«Copyright 2006, 2007 Yann Arthur Nicolas»
 *www.merlinsource.com
 *yannart@gmail.com
 *
 * This file is part of SquaresImg.
 *
 * SquaresImg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * SquaresImg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package gui;

import calc.GenerateImage;
import chooser.ImageFileChooser;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author  Yann Arthur Nicolas
 */
public class MainFrame extends javax.swing.JFrame {
    
    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        initMyComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jButtonGenerar = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanelImgPrincipal = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jButtonZoomIn = new javax.swing.JButton();
        jButtonZoomOut = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabelPorcent = new javax.swing.JLabel();
        jButtonZoom100 = new javax.swing.JButton();
        jButtonZoomAjuste = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanelColorTono = new javax.swing.JPanel();
        jRadioColorTonoGrises = new javax.swing.JRadioButton();
        jRadioColorTonoDesaturar = new javax.swing.JRadioButton();
        jRadioColorTonoMaximo = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jComboColor = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jRadioRepartAleatorio = new javax.swing.JRadioButton();
        jRadioRepartAleatorioH = new javax.swing.JRadioButton();
        jRadioRepartRegular = new javax.swing.JRadioButton();
        jButtonCargar = new javax.swing.JButton();
        jPanelImgCuadros = new javax.swing.JPanel();
        jButtonAgregar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jButtonQuitar = new javax.swing.JButton();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuArchivo = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuAyuda = new javax.swing.JMenu();
        jMenuAyudaAcerca = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SquaresImg");
        jButtonGenerar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/generar.png")));
        jButtonGenerar.setText(java.util.ResourceBundle.getBundle("lang").getString("generate"));
        jButtonGenerar.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("generate_image"));
        jButtonGenerar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonGenerarMouseClicked(evt);
            }
        });

        jSplitPane1.setResizeWeight(1.0);
        jPanelImgPrincipal.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jScrollPane1.setBackground(new java.awt.Color(204, 204, 255));
        jScrollPane1.setAutoscrolls(true);

        jButtonZoomIn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/zoomin.png")));
        jButtonZoomIn.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("zoom_in"));
        jButtonZoomIn.setBorder(null);
        jButtonZoomIn.setBorderPainted(false);
        jButtonZoomIn.setContentAreaFilled(false);
        jButtonZoomIn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonZoomInMouseClicked(evt);
            }
        });

        jButtonZoomOut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/zoomout.png")));
        jButtonZoomOut.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("zoom_out"));
        jButtonZoomOut.setBorder(null);
        jButtonZoomOut.setContentAreaFilled(false);
        jButtonZoomOut.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonZoomOutMouseClicked(evt);
            }
        });

        jLabel1.setText(java.util.ResourceBundle.getBundle("lang").getString("zoom"));

        jLabelPorcent.setText("0 %");

        jButtonZoom100.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/zoom100.png")));
        jButtonZoom100.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("zoom_real"));
        jButtonZoom100.setBorder(null);
        jButtonZoom100.setContentAreaFilled(false);
        jButtonZoom100.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonZoom100MouseClicked(evt);
            }
        });

        jButtonZoomAjuste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/zoomajuste.png")));
        jButtonZoomAjuste.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("zoom_window"));
        jButtonZoomAjuste.setBorder(null);
        jButtonZoomAjuste.setContentAreaFilled(false);
        jButtonZoomAjuste.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonZoomAjusteMouseClicked(evt);
            }
        });

        jPanelColorTono.setToolTipText("las imagenes de los cuadros se pasan a blanco y negro");
        buttonGroup3.add(jRadioColorTonoGrises);
        jRadioColorTonoGrises.setSelected(true);
        jRadioColorTonoGrises.setText(java.util.ResourceBundle.getBundle("lang").getString("luminance"));
        jRadioColorTonoGrises.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("images_equilibrated_tones"));
        jRadioColorTonoGrises.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioColorTonoGrises.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioColorTonoGrises.getAccessibleContext().setAccessibleDescription("el resultado es equilibrado: se definen los tonos de gris utilizando la luminancia");

        buttonGroup3.add(jRadioColorTonoDesaturar);
        jRadioColorTonoDesaturar.setText(java.util.ResourceBundle.getBundle("lang").getString("desaturate"));
        jRadioColorTonoDesaturar.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("images_dark_less_contrasted"));
        jRadioColorTonoDesaturar.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioColorTonoDesaturar.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup3.add(jRadioColorTonoMaximo);
        jRadioColorTonoMaximo.setText(java.util.ResourceBundle.getBundle("lang").getString("maximum_RGB"));
        jRadioColorTonoMaximo.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("images_luminus_tones"));
        jRadioColorTonoMaximo.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioColorTonoMaximo.setMargin(new java.awt.Insets(0, 0, 0, 0));

        jLabel2.setText(java.util.ResourceBundle.getBundle("lang").getString("to_black_and_white:"));

        org.jdesktop.layout.GroupLayout jPanelColorTonoLayout = new org.jdesktop.layout.GroupLayout(jPanelColorTono);
        jPanelColorTono.setLayout(jPanelColorTonoLayout);
        jPanelColorTonoLayout.setHorizontalGroup(
            jPanelColorTonoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelColorTonoLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabel2)
                .add(12, 12, 12)
                .add(jPanelColorTonoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jRadioColorTonoMaximo)
                    .add(jRadioColorTonoDesaturar)
                    .add(jRadioColorTonoGrises))
                .add(24, 24, 24))
        );
        jPanelColorTonoLayout.setVerticalGroup(
            jPanelColorTonoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelColorTonoLayout.createSequentialGroup()
                .add(jPanelColorTonoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jRadioColorTonoGrises)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jRadioColorTonoDesaturar)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jRadioColorTonoMaximo))
        );

        jComboColor.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("defines_how_calculate_color"));

        jLabel3.setText(java.util.ResourceBundle.getBundle("lang").getString("superposition_mode"));

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jComboColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 58, Short.MAX_VALUE)
                .add(jPanelColorTono, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanelColorTono, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jComboColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab(java.util.ResourceBundle.getBundle("lang").getString("color"), jPanel1);

        buttonGroup2.add(jRadioRepartAleatorio);
        jRadioRepartAleatorio.setSelected(true);
        jRadioRepartAleatorio.setText(java.util.ResourceBundle.getBundle("lang").getString("random"));
        jRadioRepartAleatorio.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("image_random"));
        jRadioRepartAleatorio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioRepartAleatorio.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup2.add(jRadioRepartAleatorioH);
        jRadioRepartAleatorioH.setText(java.util.ResourceBundle.getBundle("lang").getString("horizontal_random"));
        jRadioRepartAleatorioH.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("image_random_line"));
        jRadioRepartAleatorioH.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioRepartAleatorioH.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup2.add(jRadioRepartRegular);
        jRadioRepartRegular.setText(java.util.ResourceBundle.getBundle("lang").getString("regular"));
        jRadioRepartRegular.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("images_order"));
        jRadioRepartRegular.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioRepartRegular.setMargin(new java.awt.Insets(0, 0, 0, 0));

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jRadioRepartAleatorio)
                    .add(jRadioRepartAleatorioH)
                    .add(jRadioRepartRegular))
                .addContainerGap(368, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jRadioRepartAleatorio)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jRadioRepartAleatorioH)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jRadioRepartRegular)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jTabbedPane1.addTab(java.util.ResourceBundle.getBundle("lang").getString("distribution"), jPanel2);

        jButtonCargar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/open.png")));
        jButtonCargar.setText(java.util.ResourceBundle.getBundle("lang").getString("load"));
        jButtonCargar.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("load_principal_image"));
        jButtonCargar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCargarMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelImgPrincipalLayout = new org.jdesktop.layout.GroupLayout(jPanelImgPrincipal);
        jPanelImgPrincipal.setLayout(jPanelImgPrincipalLayout);
        jPanelImgPrincipalLayout.setHorizontalGroup(
            jPanelImgPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelImgPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelImgPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jButtonCargar)
                    .add(jPanelImgPrincipalLayout.createSequentialGroup()
                        .add(jPanelImgPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jLabel1)
                            .add(jButtonZoomAjuste, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                            .add(jButtonZoom100, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jButtonZoomOut, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jButtonZoomIn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                            .add(jLabelPorcent, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanelImgPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanelImgPrincipalLayout.setVerticalGroup(
            jPanelImgPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelImgPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelImgPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelImgPrincipalLayout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonCargar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(jPanelImgPrincipalLayout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabelPorcent)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonZoomIn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonZoomOut)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonZoom100)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonZoomAjuste)
                        .add(176, 176, 176)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 108, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanelImgPrincipalLayout.linkSize(new java.awt.Component[] {jButtonZoom100, jButtonZoomAjuste, jButtonZoomIn, jButtonZoomOut}, org.jdesktop.layout.GroupLayout.VERTICAL);

        jSplitPane1.setLeftComponent(jPanelImgPrincipal);

        jPanelImgCuadros.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), java.util.ResourceBundle.getBundle("lang").getString("squares")));
        jButtonAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.png")));
        jButtonAgregar.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("add_image"));
        jButtonAgregar.setBorder(null);
        jButtonAgregar.setContentAreaFilled(false);
        jButtonAgregar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonAgregarMouseClicked(evt);
            }
        });

        jButtonQuitar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/remove.png")));
        jButtonQuitar.setToolTipText(java.util.ResourceBundle.getBundle("lang").getString("remove_selected_images"));
        jButtonQuitar.setBorder(null);
        jButtonQuitar.setContentAreaFilled(false);
        jButtonQuitar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonQuitarMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelImgCuadrosLayout = new org.jdesktop.layout.GroupLayout(jPanelImgCuadros);
        jPanelImgCuadros.setLayout(jPanelImgCuadrosLayout);
        jPanelImgCuadrosLayout.setHorizontalGroup(
            jPanelImgCuadrosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelImgCuadrosLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jButtonAgregar)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonQuitar))
        );
        jPanelImgCuadrosLayout.setVerticalGroup(
            jPanelImgCuadrosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelImgCuadrosLayout.createSequentialGroup()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelImgCuadrosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonQuitar)
                    .add(jButtonAgregar)))
        );
        jSplitPane1.setRightComponent(jPanelImgCuadros);

        jMenuArchivo.setText(java.util.ResourceBundle.getBundle("lang").getString("file"));
        jMenuItem1.setText(java.util.ResourceBundle.getBundle("lang").getString("exit"));
        jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItem1MousePressed(evt);
            }
        });

        jMenuArchivo.add(jMenuItem1);

        jMenuBar.add(jMenuArchivo);

        jMenuAyuda.setText(java.util.ResourceBundle.getBundle("lang").getString("help"));
        jMenuAyudaAcerca.setText(java.util.ResourceBundle.getBundle("lang").getString("about"));
        jMenuAyudaAcerca.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuAyudaAcercaMousePressed(evt);
            }
        });

        jMenuAyuda.add(jMenuAyudaAcerca);

        jMenuBar.add(jMenuAyuda);

        setJMenuBar(jMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(572, Short.MAX_VALUE)
                .add(jButtonGenerar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonGenerar)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButtonCargarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCargarMouseClicked
        filechooser.setCuadros(false);
        filechooser.showOpenDialog(this);
    }//GEN-LAST:event_jButtonCargarMouseClicked
    
    private void jMenuItem1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MousePressed
        this.setVisible(false);
        this.dispose();
        System.exit(0);
    }//GEN-LAST:event_jMenuItem1MousePressed
    
    private void jMenuAyudaAcercaMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuAyudaAcercaMousePressed
        JOptionPane.showMessageDialog(this, "SquaresImg 0.1\n" +
                "Yann Arthur Nicolas\n" + "www.merlinsource.com"+ "\n2006", "SquaresImg", JOptionPane.INFORMATION_MESSAGE, new javax.swing.ImageIcon(getClass().getResource("/images/about.png")));
    }//GEN-LAST:event_jMenuAyudaAcercaMousePressed
    
    private void jButtonGenerarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonGenerarMouseClicked
        ArrayList <BufferedImage> imagenescuadros = imagecuadroslist.getData();
        BufferedImage imagenprincipal = imagepanel.getImage();
        int reparticion;
        int parametros = 0;
        int color;
        int tmpcolor = jComboColor.getSelectedIndex();
        
        switch(tmpcolor){
            case 0://multiplicacion
                color = 0;
                break;
            case 1://diferencia
                color = 3;
                break;
            case 2://quadratico
                color = 4;
                break;
            case 3://promedio
                color = 1;
                break;
            case 4://tono
                color = 2;
                if(jRadioColorTonoGrises.isSelected())
                    parametros = 0; //luminancia
                else if(jRadioColorTonoDesaturar.isSelected())
                    parametros = 1; //desaturacion
                else
                    parametros = 2; //maximo
                break;
            default:
                color = 0;
                JOptionPane.showMessageDialog(this, "Internal problem,\ncontact the programmer.", "Internal problem", JOptionPane.ERROR_MESSAGE, new javax.swing.ImageIcon(getClass().getResource("/images/error.png")));
                
        }
        
        if(jRadioRepartAleatorio.isSelected())
            reparticion = 0; //todo aleatorio
        else if(this.jRadioRepartAleatorioH.isSelected())
            reparticion = 1; //horizontal aleatorio
        else
            reparticion = 2; //regular
        
        GenerateImage generaimagen = new GenerateImage(imagenprincipal, imagenescuadros, color, reparticion, parametros);
        
        int valido = generaimagen.isValid();
        
        if(valido == 0){
            GeneratingProgress progreso = new GeneratingProgress(this, true, generaimagen);
            progreso.pack();
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            progreso.setLocation((screenSize.width-progreso.getWidth())/2,(screenSize.height-progreso.getHeight())/2);
            progreso.setVisible(true);
        }else{
            String error;
            switch(valido){
                case 1:
                    error = java.util.ResourceBundle.getBundle("lang").getString("error_no_principal_image");
                    break;
                case 2:
                    error = java.util.ResourceBundle.getBundle("lang").getString("error_no_square_image");
                    break;
                case 3:
                    error = java.util.ResourceBundle.getBundle("lang").getString("error_no_square_same_size");
                    break;
                default:
                    error = java.util.ResourceBundle.getBundle("lang").getString("error_verif_image");
            }
            JOptionPane.showMessageDialog(this, error, java.util.ResourceBundle.getBundle("lang").getString("error"), JOptionPane.ERROR_MESSAGE, new javax.swing.ImageIcon(getClass().getResource("/images/error.png")));
        }
        
    }//GEN-LAST:event_jButtonGenerarMouseClicked
    
    private void jButtonQuitarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonQuitarMouseClicked
        imagecuadroslist.removeSelected();
    }//GEN-LAST:event_jButtonQuitarMouseClicked
    
    private void jButtonAgregarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonAgregarMouseClicked
        // TODO add your handling code here:
        filechooser.setCuadros(true);
        filechooser.showOpenDialog(this);
    }//GEN-LAST:event_jButtonAgregarMouseClicked
    
    private void jButtonZoomAjusteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonZoomAjusteMouseClicked
        this.jLabelPorcent.setText(imagepanel.zoomAjuste() + " %");
    }//GEN-LAST:event_jButtonZoomAjusteMouseClicked
    
    private void jButtonZoom100MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonZoom100MouseClicked
        this.jLabelPorcent.setText(imagepanel.zoom100() + " %");
    }//GEN-LAST:event_jButtonZoom100MouseClicked
    
    private void jButtonZoomInMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonZoomInMouseClicked
        this.jLabelPorcent.setText(imagepanel.zoomIn() + " %");
    }//GEN-LAST:event_jButtonZoomInMouseClicked
    
    private void jButtonZoomOutMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonZoomOutMouseClicked
        this.jLabelPorcent.setText(imagepanel.zoomOut() + " %");
    }//GEN-LAST:event_jButtonZoomOutMouseClicked
    
    private void initMyComponents(){
        jComboColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { java.util.ResourceBundle.getBundle("lang").getString("multiplicate"), java.util.ResourceBundle.getBundle("lang").getString("difference"), java.util.ResourceBundle.getBundle("lang").getString("quadratic"), java.util.ResourceBundle.getBundle("lang").getString("average"), java.util.ResourceBundle.getBundle("lang").getString("tone") }));
        this.setIconImage((new javax.swing.ImageIcon(getClass().getResource("/images/icono.png"))).getImage());
        imagepanel = new ImagePanel(jScrollPane1);
        jScrollPane1.setViewportView(imagepanel);
        imagecuadroslist = new ImageCuadrosList();
        jScrollPane2.setViewportView(imagecuadroslist);
        filechooser = new ImageFileChooser(this);
        jPanelColorTono.setVisible(false);
        
        jComboColor.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                colorChanged(e);
            }
        });
        
    }
    private void colorChanged(ItemEvent e){
        if (e.getStateChange() == e.SELECTED) {
            if(jComboColor.getSelectedIndex() == 4)
                jPanelColorTono.setVisible(true);
            else
                jPanelColorTono.setVisible(false);
        }
    }
    
    public void muestraImagenFinal(BufferedImage bi){
        DialogImagenFinal dlgimg = new DialogImagenFinal(this, true, bi);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        dlgimg.pack();
        dlgimg.setLocation((screenSize.width-dlgimg.getWidth())/2,(screenSize.height-dlgimg.getHeight())/2);
        dlgimg.setVisible(true);
    }
    
    public void setImagenPrincipal(BufferedImage bi){
        this.jLabelPorcent.setText(imagepanel.setImage(bi) + " %");
    }
    
    public void addImagenCuadro(BufferedImage bi){
        this.imagecuadroslist.add(bi);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JButton jButtonAgregar;
    private javax.swing.JButton jButtonCargar;
    private javax.swing.JButton jButtonGenerar;
    private javax.swing.JButton jButtonQuitar;
    private javax.swing.JButton jButtonZoom100;
    private javax.swing.JButton jButtonZoomAjuste;
    private javax.swing.JButton jButtonZoomIn;
    private javax.swing.JButton jButtonZoomOut;
    private javax.swing.JComboBox jComboColor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelPorcent;
    private javax.swing.JMenu jMenuArchivo;
    private javax.swing.JMenu jMenuAyuda;
    private javax.swing.JMenuItem jMenuAyudaAcerca;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelColorTono;
    private javax.swing.JPanel jPanelImgCuadros;
    private javax.swing.JPanel jPanelImgPrincipal;
    private javax.swing.JRadioButton jRadioColorTonoDesaturar;
    private javax.swing.JRadioButton jRadioColorTonoGrises;
    private javax.swing.JRadioButton jRadioColorTonoMaximo;
    private javax.swing.JRadioButton jRadioRepartAleatorio;
    private javax.swing.JRadioButton jRadioRepartAleatorioH;
    private javax.swing.JRadioButton jRadioRepartRegular;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    private ImagePanel imagepanel;
    private ImageCuadrosList imagecuadroslist;
    private ImageFileChooser filechooser;
    //private  = null;
}
